package com.games.game.mapper;

import com.games.game.bean.UserInfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserMapper {
    static String JDBC_URL =  "jdbc:mysql://localhost/games?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";//?allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8";
    static String JDBC_USER = "root";
    static String JDBC_PASSWORD = "root";

    public UserInfo logIn(String name, String password) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
        try (PreparedStatement ps = conn.prepareStatement(
            "SELECT * FROM users_info WHERE `name` = ? and password = ?")) {
            conn.setAutoCommit(false);
            ps.setObject(1, name);
            ps.setObject(2, password); // name
            ResultSet rs = ps.executeQuery();
            conn.commit();
            conn.setAutoCommit(true);
            while (rs.next()) {
                int id = rs.getInt(1);
                String u_name = rs.getString(2);
                String u_password = rs.getString(3);

                UserInfo userInfo = new UserInfo(u_name, u_password, 0, "None");
                userInfo.setId(id);
                return userInfo;
            }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void register(String name, String password) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "INSERT users_info VALUE (null, ?, ?, 2, ?)")) {
                conn.setAutoCommit(true);
                ps.setObject(1, name);
                ps.setObject(2, password);
                ps.setObject(3, "None");
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void modify_password(String name, String password) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "UPDATE users_info SET password = ? WHERE name = ?")) {
                ps.setObject(1, password);
                ps.setObject(2, name);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void update_photo(String username, String photo) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "UPDATE users_info SET photo = ? WHERE name = ?")) {
                ps.setObject(1, username);
                ps.setObject(2, username);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void load() {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations
            Class.forName("com.mysql.cj.jdbc.Driver");
//            System.out.println("加载成功");
        } catch (Exception ex) {
            System.out.println("加载失败");
            // handle the error
        }
    }
}
